using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SEAM.Core;

namespace SEAM.Data
{
    /// <summary>
    /// Defines a class for DataAccessParam that represents paraneters
    /// that can be used to execute database commands.
    /// </summary>
    public class DataAccessParam
    {
        #region Member Variables
        private string _Name;
        private ParamType _Type;
        private object _Value;
        private int? _Size;
        private ParameterDirection _ParamDirection = ParameterDirection.Input;
        #endregion

        #region Properties

        /// <summary>
        /// Name of the DataAccessParam
        /// </summary>
        public string Name
        {
            get
            {
                return _Name;
            }
        }

        /// <summary>
        /// Type of the DataAccessParam
        /// </summary>
        public ParamType Type
        {
            get
            {
                return _Type;
            }
        }

        /// <summary>
        /// Value of the DataAccessParam
        /// </summary>
        public object Value
        {
            get
            {
                return _Value;
            }
            set
            {
                if (Value == DBNull.Value)
                    _Value = null;
                _Value = value;
            }
        }

        /// <summary>
        /// Size of the DataAccessParam
        /// </summary>
        public int? Size
        {
            get { return _Size; }
        }

        /// <summary>
        /// Checks to see if the Size was specified for the DataAccessParam
        /// </summary>
        public bool IsSizeSpecified
        {
            get { return (Size.HasValue); }
        }

        /// <summary>
        /// Checks to see if the Value was specified for the DataAccessParam
        /// </summary>
        public bool IsValueSpecified
        {
            get { return (Value != null); }
        }

        /// <summary>
        /// Direction of the DataAccessParam
        /// </summary>
        public ParameterDirection ParamDirection
        {
            get
            {
                return _ParamDirection;
            }
            set
            {
                _ParamDirection = value;
            }
        }

        /// <summary>
        /// <c>string</c> that represents the declaration of the DataAccessParam
        /// </summary>
        public string DeclarationString
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(Type.ToString() + " ");
                sb.Append(Name + " ");

                if (ParamDirection != ParameterDirection.Input)
                    sb.Append(ParamDirection);

                if (IsSizeSpecified)
                    sb.Append("(" + Size + ")");

                return sb.ToString();
            }
        }

        /// <summary>
        /// <c>string</c> that represents the Value of the DataAccessParam
        /// </summary>
        public string ValueString
        {
            get
            {
                if (IsValueSpecified)
                    return Value.ToString();
                else
                    return "[null]";
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes an instance of DataAccessParam class with a name
        /// and type.
        /// </summary>
        /// <param name="sName">Name of the parameter</param>
        /// <param name="ptType">Type of the parameter</param>
        public DataAccessParam(string sName, ParamType ptType)
            : this(sName, ptType, null, null)
        {
        }

        /// <summary>
        /// Initializes an instance of DataAccessParam class with a name,
        /// type, and size.
        /// </summary>
        /// <param name="sName">Name of the parameter</param>
        /// <param name="ptType">Type of the parameter</param>
        /// <param name="iSize">Size of the parameter</param>
        public DataAccessParam(string sName, ParamType ptType, int? iSize)
            : this(sName, ptType, iSize, null)
        {
        }

        /// <summary>
        /// Initializes an instance of DataAccessParam class with a name,
        /// type, and value.
        /// </summary>
        /// <param name="sName">Name of the parameter</param>
        /// <param name="ptType">Type of the parameter</param>
        /// <param name="objValue">Value of the parameter</param>
        public DataAccessParam(string sName, ParamType ptType, object objValue)
            : this(sName, ptType, null, objValue)
        {
        }

        /// <summary>
        /// Initializes an instance of DataAccessParam class with a name,
        /// type, and value.
        /// </summary>
        /// <param name="sName">Name of the parameter</param>
        /// <param name="ptType">Type of the parameter</param>
        /// <param name="iSize">Size of the parameter</param>
        /// <param name="objValue">Value of the parameter</param>
        public DataAccessParam(string sName, ParamType ptType, int? iSize, object objValue)
        {
            _Name = sName;
            _Type = ptType;
            _Size = iSize;
            _Value = objValue;
        }

        #endregion

        /// <summary>
        /// Creates and returns a string representation of the current DataAccessParam.
        /// </summary>
        /// <returns>A string representation of the current DataAccessParam.</returns>
        public override string ToString()
        {
            return DeclarationString + " = " + ValueString;
        }

    }
}
